﻿using RzCodeGenerator.Core;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using static System.Net.Mime.MediaTypeNames;

namespace WeifenLuoWinFormDemo2
{
    public partial class MainFrm : Form
    {
        //以各种方式停靠
        BaseFrm navFrm = new NavFrm();
        BaseFrm navPdFrm = new NavPdFrm();
        BaseFrm fun1 = new StartFrm();

        BaseFrm testFrm1 = new TestFrm1();


        private ToolStripSeparator toolStripSeparator;


        public MainFrm()
        {
            InitializeComponent();

            InitStatusStrip();
            InitDockPanel();
            LoadFrm();
        }


        /// <summary>
        /// 初始化 statusStrip
        /// </summary>
        private void InitStatusStrip()
        {
            toolStripSeparator = new ToolStripSeparator();

            // 添加分隔线到statusStrip
            statusStrip1.Items.Add(toolStripSeparator);            

            // 添加标签到statusStrip
            statusStrip1.Items.Add("就绪");
        }


        /// <summary>
        /// 初始化 DockPanel 
        /// </summary>
        private void InitDockPanel() 
        {
            this.IsMdiContainer = true;

            //改变主题
            VS2015LightTheme vs2015 = new VS2015LightTheme();
            //VS2013LightTheme vs2013 = new VS2013LightTheme();
            //VS2012LightTheme vs2012 = new VS2012LightTheme();
            //VS2005Theme vs2005 = new VS2005Theme();
            //VS2003Theme vs2003 = new VS2003Theme();
            dockPanel1.Theme = vs2015;
            dockPanel1.DockLeftPortion = 0.12;  // left panel的宽度
            dockPanel1.DockRightPortion = 0.12;  // left panel的宽度
        }

        private void LoadFrm()
        {
            navFrm.TabText = "数据库视图";
            navPdFrm.TabText = "PowerDesigner视图";

            //navFrm.Icon = Properties.Resources.viewDb1;

            // 数据库导航窗口
            navFrm.HideOnClose = true;
            navFrm.DockStateChanged += NavFrm_DockStateChanged;
            //navFrm.ChildFormClosed += NavFrm_ChildFormClosed;
            navFrm.Show(dockPanel1, DockState.DockLeft);
            
            // PowerDesigner导航窗口
            // 设置PdFrm窗口的HideOnClose = true;窗口隐藏时不关闭销毁窗口
            // Pd窗口的DockStateChanged时间进行订阅：NavPdFrm_DockStateChanged;
            navPdFrm.HideOnClose = true;
            navPdFrm.DockStateChanged += NavPdFrm_DockStateChanged;
            navPdFrm.Show(dockPanel1, DockState.DockRight);


            fun1.Show(dockPanel1, DockState.Document);
            fun1.ControlBox = false;
            fun1.CloseButtonVisible = false;


            testFrm1.Show(dockPanel1, DockState.Document);
            testFrm1.ControlBox = false;
            testFrm1.CloseButtonVisible = false;

        }



        private void NavFrm_DockStateChanged(object sender, EventArgs e)
        {
            // 处理子窗口“显示/隐藏”状态的通知            
            //MessageBox.Show("子窗口已经关闭");            
            NavFrmStatusToggle();
        }


        private void NavPdFrm_DockStateChanged(object sender, EventArgs e)
        {
            // 处理子窗口“显示/隐藏”状态的通知            
            //MessageBox.Show("子窗口已经关闭");            
            NavPdFrmStatusToggle();
        }

        private void tsmiViewDb_Click(object sender, EventArgs e)
        {
            NavFrmStatusToggle();
        }


        private void tsmiViewPd_Click(object sender, EventArgs e)
        {
            NavPdFrmStatusToggle();
        }


        #region 菜单/ToolBar.Button/DockForm窗口 隐藏 联动状态

        private void NavFrmStatusToggle()
        {
            DockStatusToggle(navFrm, tsmiViewDb, tsbtnViewDb);
        }


        private void NavPdFrmStatusToggle()
        {
            DockStatusToggle(navPdFrm, tsmiViewPd, tsbtnViewPd);
        }

        /// <summary>
        /// 菜单/ToolBar.Button/DockForm窗口 隐藏 联动状态
        /// </summary>
        /// <param name="_frm">DockForm窗口</param>
        /// <param name="_tsmi">菜单</param>
        /// <param name="_tsbtn">ToolBar.Button</param>

        private void DockStatusToggle(BaseFrm _frm, ToolStripMenuItem _tsmi, ToolStripButton _tsbtn)
        {
            bool isShow = false;

            if (_tsmi.Checked)
            {
                _frm.Hide();
            }
            else
            {
                _frm.Show();
                _frm.Focus();
                isShow = true;
            }

            _tsmi.Checked = _tsbtn.Checked = isShow;
        }
        
        #endregion


        private void tsmiSystemDbConfig_Click(object sender, EventArgs e)
        {
            DbSetFrm dbSetFrm = new DbSetFrm();
            dbSetFrm.ShowDialog();
        }



        #region 关闭窗口更新状态，暂时不用！！！
        private void NavFrm_ChildFormClosed(object sender, EventArgs e)
        {
            // 处理子窗口关闭的通知            
            //MessageBox.Show("子窗口已经关闭");            
            NavFrmStatusToggle();
        }

        private void NavFrmStatusToggleByFormClose()
        {
            bool isShow = false;

            if (this.tsmiViewDb.Checked)
            {
                if (!navFrm.IsDisposed) navFrm.Hide();
            }
            else
            {
                // 判断 navFrm 如果已释放 --> 创建新的实例
                if (navFrm.IsDisposed)
                {
                    navFrm = new NavFrm();
                    navFrm.ChildFormClosed += NavFrm_ChildFormClosed;
                    navFrm.Show(dockPanel1, DockState.DockLeft);
                }
                // 判断 navFrm 如果未释放 --> 直接显示，获取焦点
                else
                {
                    navFrm.Show();
                    navFrm.Focus();
                }
                isShow = true;
            }

            this.tsmiViewDb.Checked = this.tsbtnViewDb.Checked = isShow;
        }
        #endregion
    }
}

